﻿using DLL.NET.DB.MS.Drugs.CRUD.Service;
using DLL.NET50.DB.MS.ORM.Service;
using DLL.NET60.DB.MS.Entity.DTO;
using DLL.NET70.Infrastructure.Prism.MVVM;
using DLL.Standard.Infrastructure.Entity;
using Prism.Commands;
using Prism.Events;
using Prism.Mvvm;
using Prism.Regions;
using PrismModule.MS.WorkFile;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Windows;

namespace PrismModule.WorkFile.ViewModels
{
    public class PUC_ShowWorkFileViewModel : CMVVMBase, IRegionMemberLifetime
    {
        #region navigate

        public bool KeepAlive
        {
            get
            {
                //return false;//这个的作用是，只要离开当前切面，就注销掉当前页面
                return false;//这个的作用是，只要离开当前切面，就注销掉当前页面
            }
        }

        #endregion navigate

        #region window load

        //public DelegateCommand LoadedCommand { get; private set; }
        //public DelegateCommand UnloadedCommand { get; private set; }

        public override void Loaded()
        {
            RegisterCommandMsgReceive();
            l_Object = _workFileService.GetAllWorkFile();
            base.Loaded();
        }

        public override void UnLoaded()
        {
            UnRegisterCommandMsgReceive();
            base.UnLoaded();
        }

        #endregion window load

        #region datagrid 显示变量

        private List<DTO_adi_work_file> _l_Object = new List<DTO_adi_work_file>();

        public List<DTO_adi_work_file> l_Object
        {
            get { return _l_Object; }
            set
            {
                SetProperty(ref _l_Object, value);
            }
        }

        private DTO_adi_work_file _selectObject = new DTO_adi_work_file();

        public DTO_adi_work_file SelectObject
        {
            get { return _selectObject; }
            set
            {
                SetProperty(ref _selectObject, value);
            }
        }

        #endregion datagrid 显示变量

        #region 加工文件查询变量

        private string _edrugCode;

        public string EDrugCode
        {
            get { return _edrugCode; }
            set
            {
                SetProperty(ref _edrugCode, value);
            }
        }

        private string _edrugName;

        public string EDrugName
        {
            get { return _edrugName; }
            set
            {
                SetProperty(ref _edrugName, value);
            }
        }

        private string _edrugPYCode;

        public string EDrugPYCode
        {
            get { return _edrugPYCode; }
            set
            {
                SetProperty(ref _edrugPYCode, value);
            }
        }

        #endregion 加工文件查询变量

        #region 加工文件编辑变量

        private DTO_adi_work_file _editObject = new DTO_adi_work_file();

        public DTO_adi_work_file EditObject
        {
            get { return _editObject; }
            set
            {
                SetProperty(ref _editObject, value);
                RaisePropertyChanged();
            }
        }

        #endregion 加工文件编辑变量

        private IWorkFileService _workFileService;
        private IMSDrugsService _drugsService;

        public PUC_ShowWorkFileViewModel(
            IRegionManager regionManager,
            IEventAggregator eventAggregator,

            IWorkFileService workFileService,
            IMSDrugsService drugsService) : base(regionManager, eventAggregator)
        {
            _workFileService = workFileService;
            _drugsService = drugsService;

            //LoadedCommand = new DelegateCommand(Loaded);
            //UnloadedCommand = new DelegateCommand(UnLoaded);

            AddCommand = new DelegateCommand(Add);
            RefreshCommand = new DelegateCommand(Refresh);
            SearchCommand = new DelegateCommand(Search);
            EditCommand = new DelegateCommand(Edit);
            DeleteCommand = new DelegateCommand(Delete);
        }

        #region 接收命令消息消息

        public override void CommandMessageReceived(CCommandMessage message)
        {
            if (message.CommandStr == GlobalCommand.SaveOK)
            {
                update();
            }
            base.CommandMessageReceived(message);
        }

        #endregion 接收命令消息消息

        #region delegateCommand

        public DelegateCommand AddCommand { get; set; }
        public DelegateCommand RefreshCommand { get; set; }
        public DelegateCommand SearchCommand { get; set; }
        public DelegateCommand EditCommand { get; set; }
        public DelegateCommand DeleteCommand { get; set; }
        //读取本地所有已经设置过药品属性的药品

        public void update()
        {
            //l_Object.Clear();
            //List<DTO_adi_work_file> lobj = _workFileService.GetAll();
            //lobj.ForEach(p => l_Object.Add(p)); //遍历list添加到EmployeeList中
            l_Object = _workFileService.GetAllWorkFile();
        }

        private void Add()
        {
            EditObject = new DTO_adi_work_file();
            NavigateToEditWorkFile(EditObject);
        }

        private void Refresh()
        {
            update();
        }

        private void Search()
        {
            l_Object = _workFileService.WfWorkFile(EDrugCode, EDrugPYCode, EDrugName);
        }

        private void Edit()
        {
            //NavigateToEditWorkFile(SelectObject);
            SendEditWorkFileEvent(SelectObject);
        }

        private void NavigateToEditWorkFile(DTO_adi_work_file wf)
        {
            var parameters = new NavigationParameters();
            if (wf != null)
            {
                parameters.Add("Workfile", wf);
                string navigatePath = "PUC_AddWorkFile";
                RegionManager.RequestNavigate("EditWorkFileContent", navigatePath, parameters);
                Message = "进入配药文件编辑界面";
            }
        }

        private void SendEditWorkFileEvent(DTO_adi_work_file obj)
        {
            EventAggregator.GetEvent<CEditWorkFileEvent>().Publish(obj);
        }

        private void Delete()
        {
            if (MessageBoxResult.OK == MessageBox.Show("你确定要删除该药品信息吗?", "Confirm Message", MessageBoxButton.OKCancel))
            {
                _workFileService.DeleteWorkFile(SelectObject);
                update();
                if (null == l_Object)
                {
                    MessageBox.Show("获取加工文件出现异常");
                }
            }
        }

        #endregion delegateCommand
    }
}